﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 系统设置
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.15 
    /// </summary>
    public class SysConfigBll
    {
        private SysConfigIService service = new SysConfigService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="isCache">默认缓存开启</param>
        /// <returns></returns>
        public async Task<List<SysConfigEntity>> GetList(bool isCache = true)
        {
            try
            {
                var list = new List<SysConfigEntity>();
                if (isCache == true)
                {
                    var cacheKey = CacheKey.SYSTEMINFO;
                    if (!CacheFactory.Cache.Exists(cacheKey))
                    {
                        list = await service.GetList();
                        CacheFactory.Cache.Insert(cacheKey, list);
                    }
                    else
                    {
                        list = CacheFactory.Cache.Get<List<SysConfigEntity>>(cacheKey);
                    }
                }
                else
                {
                    list = await service.GetList();
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <returns></returns>
        public async Task<BaseSystemInfo> GetInfo(string type)
        {
            try
            {
                var objModel = new Dictionary<string, string>();
                var data = (await this.GetList()).FindAll(x => x.Category.Equals(type));
                foreach (var item in data)
                {
                    objModel.Add(item.Key, item.Value);
                }
                var objJson = objModel.ToJson();
                return objJson.ToObject<BaseSystemInfo>();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Save(List<SysConfigEntity> entitys, string category)
        {
            try
            {
                var cacheKey = CacheKey.SYSTEMINFO;
                CacheFactory.Cache.Remove(cacheKey);
                await service.Save(entitys, category);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Update(List<SysConfigEntity> entitys)
        {
            try
            {
                var cacheKey = CacheKey.SYSTEMINFO;
                CacheFactory.Cache.Remove(cacheKey);
                await service.Update(entitys);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
